import { message } from 'antd';

/**
 * 获取地址栏传值
 * @param paramName 参数名称
 * @returns
 */
export function getQueryString(paramName: string) {
  const queryString = window.location.search.substr(1);
  const aPairs = queryString.split('&');

  for (let i = 0; i < aPairs.length; i++) {
    const aTmp = aPairs[i].split('=');
    if (aTmp[0] === paramName) {
      return aTmp[1];
    }
  }
  return '';
}

/**
 * 判断设备是不是移动端
 */
export function isMobile() {
  const flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i,
  );
  return !!flag;
}

function getPrefixed(name: string): (...rest: any[]) => void {
  return (window as any)['webkit' + name] || (window as any)['moz' + name] || (window as any)['ms' + name];
}

// fallback for IE 7-8
let lastTime = 0;

function timeoutDefer(fn: () => void) {
  const time = +new Date();
  const timeToCall = Math.max(0, 16 - (time - lastTime));

  lastTime = time + timeToCall;
  return window.setTimeout(fn, timeToCall);
}

/**
 * 运行动画
 */
export const requestFn: (func: any) => number =
  window.requestAnimationFrame || (getPrefixed('RequestAnimationFrame') as any) || timeoutDefer;

/**
 * 取消运行动画
 */
export const cancelFn: (id: number) => void =
  window.cancelAnimationFrame ||
  getPrefixed('CancelAnimationFrame') ||
  getPrefixed('CancelRequestAnimationFrame') ||
  function (id) {
    window.clearTimeout(id);
  };

/**
 * 赋值内容到剪切板
 */
export function copyToClipboard(value: string) {
  if (!value) {
    return;
  }
  const input = document.createElement('input');
  // input.style.display = 'none';
  document.body.appendChild(input);

  input.value = value;
  input.select();

  try {
    // 复制
    if (!document.execCommand('copy', false, null as any)) {
      message.error('复制失败！');
    } else {
      message.success('复制成功！');
    }
  } finally {
    document.body.removeChild(input);
  }
}

/**
 * 构造一个定时器方法
 * @param func
 * @param interval 单位 秒
 * @param execAtFirst 执行第一次
 * @returns
 */
export function newTimer(func: (pass: number, start: number, now: number) => void, interval = 1, execAtFirst = false) {
  let isEnd = false;
  const startTime = Date.now();
  // 更新方法
  const update = () => {
    if (isEnd) return;

    // 已经经过的时间
    const nowTime = Date.now();
    const passTime = (nowTime - startTime) / 1000;
    func(passTime, startTime, nowTime);
  };
  const timer = setInterval(update, interval * 1000);
  // 是否在第一次就执行
  if (execAtFirst) update();

  return {
    cancel(exec = false) {
      exec && update();
      isEnd = true;
      clearInterval(timer);
    },
  };
}

/**
 * 构造平滑动画
 * @param func 动画帧
 * @param autoUpdate 是否自动运行下一帧
 * @returns
 */
export function animate(func: (update: () => void) => void, autoUpdate = true) {
  let running = true;
  let animateId: any = null;

  // 更新方法
  const update = function () {
    if (!running) return;

    animateId = requestFn(() => {
      func(update);
      autoUpdate && update();
    });
  };

  // 启动动画
  update();

  return {
    cancel() {
      running = false;
      cancelFn(animateId);
      animateId = null;
    },
  };
}
